package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"role:view","角色页面"},logical = Logical.OR)
    public String view() throws Exception{

        return "role/role";
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Role> selectAll() throws Exception{
        return roleService.selectAll();
    }

    @RequestMapping("selectRoleByEmpId")
    @ResponseBody
    public List<Long> selectRoleByEmpId(Long id) throws Exception{
        return roleService.selectRoleByEmpId(id);
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"role:saveOrUpdate","角色新增/修改"},logical = Logical.OR)
    public JsonResult saveOrUpdate(Role entity) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            roleService.saveOrUpdate(entity );
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    @RequestMapping("delete")
    @ResponseBody
    @RequiresPermissions(value = {"role:delete","角色删除"},logical = Logical.OR)
    public JsonResult delete(Long id) throws Exception{
        JsonResult jsonResult = new JsonResult();
        try{
            roleService.deleteByPrimaryKey(id);
        }catch (Exception e){
            jsonResult.mark("操作失败");
        }
        return jsonResult;
    }
    

    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) throws Exception{
        return roleService.query(qo);
    }

}
